package com.poizon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.poizon.config.SystemConfig;
import com.poizon.dao.MomentCommentDao;
import com.poizon.dao.RecordMomentDao;
import com.poizon.dto.MomentCommentDto;
import com.poizon.entity.MomentComment;
import com.poizon.entity.RecordMoment;
import com.poizon.service.intf.MomentCommentService;
import com.poizon.service.intf.RecordMomentService;
import com.poizon.utils.BeanUtils;
import com.poizon.utils.CheckUtils;
import com.poizon.vo.MomentCommentVo;
import com.poizon.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName MomentCommentServiceImpl
 * @Author NoCheng
 * @Date 2022/1/5 21:08
 * @Description:TODO 动态评论业务实现层
 */
@Service
public class MomentCommentServiceImpl implements MomentCommentService {
    private final MomentCommentDao dao;
    private final RecordMomentDao momentDao;

    public MomentCommentServiceImpl(MomentCommentDao dao, RecordMomentDao momentDao) {
        this.dao = dao;
        this.momentDao = momentDao;
    }

    @Override
    @Transactional
    public R add(MomentCommentDto dto) {
        if (CheckUtils.checkComment(dto.getCommentContent())){
            MomentComment comment = BeanUtils.beanConvert(MomentComment.class, dto, dto.getClass().getDeclaredFields());
            dao.insert(comment);
            UpdateWrapper<RecordMoment> wrapper = new UpdateWrapper<RecordMoment>().setSql("comment_number = comment_number + 1")
                    .set("comment_time", new Date()).eq("id", dto.getMomentId());
            momentDao.update(null, wrapper);
            return R.ok();
        }
        return R.fail("非法请求");
    }

    @Override
    public R del(Long id) {
        UpdateWrapper<MomentComment> wrapper = new UpdateWrapper<>();
        wrapper.set("flag", SystemConfig.INVALID).eq("id", id);
        dao.update(null, wrapper);
        return R.ok();
    }

}
